<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Member_Model extends CI_Model {
	
	public function __construct() {
		$this->db->cache_delete_all();
		$this->load->helper("public");
		$this->load->helper("ip_helper");
	}
	/**
	 * [select_member_by_email 通过email]
	 * @param  [type] $email [description]
	 * @return [type]        [description]
	 */
	public function select_one_by_field($field, $value) {
		$this->db->select('*');
		$this->db->where($field, $value);
		$this->db->from('member');

		$query = $this->db->get();

		if ($query->num_rows() > 0) {
			return $query->row_array();
		} else {
			return FALSE;
		}
	}

	public function isFailedTooMuch($passid) {
		$this->db->select('*');
		$this->db->where('passid', $passid);
		$this->db->from('member_failedlogin');

		$query = $this->db->get();

		if ($query->num_rows() > 0) {
			$result = $query->row_array();

			if ($result['lasttime'] < strtotime(date('Y-m-d')) ) {
				$data = array(
					'times' => 0,
					'lasttime' => strtotime(date('Y-m-d'))
					);
				$this->db->where('passid', $passid);
				$this->db->update('member_failedlogin', $data);
				return FALSE;
			}

			if ($result['times'] >= $this->config->item('maxFailedTimes')) {
				return TRUE;
			} else {
				return FALSE;
			}
		} else {
			return FALSE;
		}
	}

	public function add_failedlogin_times($passid) {
		//需要添加false，防止被转义
		$this->db->set('times', 'times + 1', FALSE);
		$this->db->where('passid', $passid);
		$this->db->update('member_failedlogin');
		$query = $this->db->get_where('member_failedlogin', array('passid' => $passid));
		$result = $query->row_array();
		return $this->config->item('maxFailedTimes') - intval($result['times']);
	}

	/**
	 * [update_member_status 更新用户的登陆次数登陆时间等]
	 * @param  [type] $passid [description]
	 * @return [type]         [description]
	 */
	public function update_member_status($passid) {
		$this->load->helper('ip');
		//需要添加false，防止被转义
		$this->db->set('logintimes', 'logintimes + 1', FALSE);
		$this->db->set('lastlogintime', time());
		$this->db->set('lastloginip', get_client_ip());
		$this->db->where('passid', $passid);
		$this->db->update('member_status');
	}

	public function get_default_role($passid) {
		$this->db->select('*');
		$this->db->where('pass_id', $passid);
		$this->db->where('isdefault', 1);

		$query = $this->db->get('member_role');

		if ($query->num_rows() > 0) {
			return $query->row_array();
		} else {
			return FALSE; 
		}
	}
	
	public function get_roles_num($passid) {
		$this->db->select('*');
		$this->db->where('pass_id', $passid);
		$query = $this->db->get('member_role');
		
		return $query->num_rows();
	}

	public function get_profile($passid) {
		$this->db->select('*');
		$this->db->where('passid', $passid);
		//$this->db->where('realname', $realname);
		$this->db->from('member_profile');

		$query = $this->db->get();

		if ($query->num_rows() > 0) {
			return $query->row_array();
		} else {
			return FALSE;
		}
	}

	public function insert_profile($result) {
		$data =array(
			'realname' => $result['user_name'],
			'idcard'   => $result['user_id'],
			'company'  => $result['user_depart'],
			'isused'   => 1
			);
		if (!empty($result['user_birth'])) {
			$timestamp = strtotime($result['user_birth']);
			$data['birthyear'] = date('Y', $timestamp);
			$data['birthmonth'] = date('m', $timestamp);
			$data['birthday'] = date('d', $timestamp);
		}

		$this->db->insert('member_profile', $data);
		return $this->db->insert_id();
	}

	public function clear_login_failed($passid) {
		$ths->db->set("times", "0");
		$this->db->where("passid", $passid);
		return $this->db->update("member_failedlogin");
	}
	public function insert_member($result) {
		$this->db->insert('member', $result);
		$passid = $this->db->insert_id();
		if (empty($passid)) {
			return FALSE;
		}

		return $passid;
	}

	public function create_role($roledata) {
		$this->db->insert('member_role', $roledata);
		return $this->db->insert_id();
	}

	public function bind_lib_account($password) {
		$this->db->select("*");
		$this->db->where("passid", $_SESSION["passid"]);
		$query = $this->db->get("member_libaccount");

		if ($query->num_rows() > 0) {
			$this->db->set("password", $password);
			$this->db->where("passid", $_SESSION["passid"]);
			return $this->db->update("member_libaccount");
		} else {
			$data = array(
				"passid" => $_SESSION["passid"],
				"password" => $password,
				"updatetime" => time(),
				"logintimes" => 0
				);
			return $this->db->insert('member_libaccount', $data);
		}
	}

	public function get_lib_password($passid) {
		$this->db->select("*");
		$this->db->where("passid", $passid);
		$query = $this->db->get("member_libaccount");

		if ($query->num_rows() > 0) {
			return $query->row_array();
		} else {
			return FALSE;
		}
	}

	/*
	判断来源ip是否在被禁的表中
	 */
	public function check_ip_banned($passid, $ip) {
		$gtm_expired = time() - $this->config->item("ip_expired");
		$this->db->select("*");
		$this->db->where("ip", $ip);
		$this->db->where("pass_id", $passid);
		$this->db->where("gmt_banned >=", $gtm_expired);
		$query = $this->db->get("member_ipbanned");

		if ($query->num_rows() > 0) {
			return true;
		} else {
			return false;
		}
	}

	public function get_last_login_fails($passid, $ip) {
		$gmt_violence = time() - intval($this->config->item("gmt_violence"));

		$this->db->select("*");
		$this->db->where("pass_id", $passid);
		$this->db->where("ip", $ip);
		$this->db->where("gmt_login >=", $gmt_violence);
		$this->db->where("success", 1);
		$this->db->order_by("gmt_login", "desc");
		$query = $this->db->get("member_log");

		if ($query->num_rows() > 0) {
			$result = $query->row_array();
			$gmt_last_success = $result["gmt_login"];
		} else {
			$gmt_last_success = $gmt_violence;
		}


		$sql = "SELECT count(*) AS `failedTimes` FROM `pass_member_log` WHERE `pass_id` = ? AND `gmt_login` > ? AND `success` = '0'";
		$query = $this->db->query($sql, array($passid, $gmt_last_success));
		return $query->row_array();
	}

	/*
	登录日志插入，包括失败和成功，默认插入成功的
	 */
	public function insert_log($passid, $ip, $success = "1") {
		$data = array(
			"pass_id" => $passid,
			"ip" => $ip,
			"success" => $success
			);
		return $this->db->insert("member_log", $data);
	}

	public function insert_ipbanned($passid, $ip){
		$data = array(
			"pass_id" => $passid,
			"ip" => $ip,
			);
		return $this->db->insert("member_ipbanned", $data);
	}
}